import Vue from 'vue'
import Vuex from 'vuex'
import { pathNormalize } from '../utils/BaseUtil'

Vue.use(Vuex)

/**
 * require.context(directory, useSubdirectories = false, regExp = /^\.\//);
 * @see https://webpack.docschina.org/guides/dependency-management/
 */
class StoreModule {
    baseStoreModulesContext() {
        return require.context('./modules', true, /\.js$/)
    }
    getBaseStoreModules() {
        const context = this.baseStoreModulesContext()
        const modules = {}
        context.keys().forEach(modulePath => {
            const moduleName = modulePath.replace(/^\.\/(.*)\.\w+$/, '$1')
            const _module = context(modulePath)
            modules[moduleName] = _module.default || _module
        })
        return modules
    }
    getModuleFilesByContext(context, prefixPath) {
        return context.keys().map(modulePath => pathNormalize(prefixPath + modulePath))
    }
    getModuleFiles() {
        return this.getModuleFilesByContext(this.baseStoreModulesContext(), './modules/')
    }
    getModules() {
        return this.getBaseStoreModules()
    }
}
const storeModule = new StoreModule()

const store = new Vuex.Store({
    // strict: process.env.NODE_ENV !== 'production',
    // plugins: process.env.NODE_ENV !== 'production' ? [createLogger()] : [],
    state: {
    },
    mutations: {
    },
    actions: {

    },
    getters: {

    },
    modules: storeModule.getModules()
})

export default store
